package com.example.demo.mvvm.ui.vm.activity;

import android.content.Intent;
import android.view.View;
import android.widget.Toast;

import com.example.demo.mvvm.bean.UserBean;
import com.example.demo.mvvm.databinding.ActivityLoginBinding;
import com.example.demo.mvvm.net.BaseObserver;
import com.example.demo.mvvm.net.NetRequest;
import com.example.demo.mvvm.ui.activity.MainActivity;
import com.example.demo.mvvm.ui.base.BaseViewVM;
import com.example.demo.mvvm.utils.ActivityUtils;
import com.example.demo.mvvm.utils.UserManager;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

/**
 * Created by 80711 on 2018/5/22.
 */

public class LoginActivityVm extends BaseViewVM{
    private ActivityLoginBinding binding;

    public LoginActivityVm(ActivityLoginBinding binding){
        this.binding = binding;
    }

    public void register(View view){
        NetRequest.register(binding.username.getText().toString(), binding.password.getText().toString(), new BaseObserver<UserBean>(ActivityUtils.peek() , false) {
            @Override
            public void onSuccess(UserBean userBean, RefreshLayout refreshLayout) {
                Toast.makeText(ActivityUtils.peek(), "登录成功", Toast.LENGTH_SHORT).show();
                UserManager.saveUser(userBean);
                ActivityUtils.push(ActivityUtils.peek() , MainActivity.class , new Intent().setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            }

            @Override
            public void onFailure(int code, String message, RefreshLayout refreshLayout) {
                Toast.makeText(ActivityUtils.peek(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void login(View view){
        NetRequest.login(binding.username.getText().toString(), binding.password.getText().toString(), new BaseObserver<UserBean>(ActivityUtils.peek() , false) {
            @Override
            public void onSuccess(UserBean userBean, RefreshLayout refreshLayout) {
                Toast.makeText(ActivityUtils.peek(), "登录成功", Toast.LENGTH_SHORT).show();
                UserManager.saveUser(userBean);
                ActivityUtils.push(ActivityUtils.peek() , MainActivity.class , new Intent().setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
            }

            @Override
            public void onFailure(int code, String message, RefreshLayout refreshLayout) {
                Toast.makeText(ActivityUtils.peek(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }

}
